package com.hongdee.insurance.car.controller;

import com.alibaba.druid.util.StringUtils;
import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.EntityUtils;
import com.hongdee.insurance.car.entity.TInsuranceCar;
import com.hongdee.insurance.car.service.ITInsuranceCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 名  称:TInsuranceCarController
 * 类  型:class
 * 描  述:
 * 路  径:com.hongdee.insurance.car.controller
 * 作  者: 术洪超
 * 日  期:2017-04-25 10:06
 */

@RestController
@RequestMapping("/insurance/car")
public class TInsuranceCarController {

    @Autowired
    private ITInsuranceCarService itInsuranceCarService;


    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param start 起始记录
     * @param limit 页面大小
     * @param data  参数数组
     * @return Map
     * @throws Exception
     * @desc 通过T_INSURANCE_CAR表里的车架号vin、车牌号plateNum、初次登记日期firstRecordDate、联系人contactName分页查询保险车辆档案
     * @author 术洪超
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestBody Map data) throws Exception {

        Map map = itInsuranceCarService.page(data, start, limit, uid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param data  要保存的车辆实体
     * @return Map
     * @throws Exception
     * @desc 保存添加或者修改的实体内容，通过主键判断
     * @author 术洪超
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TInsuranceCar data) throws Exception {
        EntityUtils.valid(data);
        TInsuranceCar car = this.itInsuranceCarService.saveOrUpdate(data, uid);
        ResultEntity res = new ResultEntity();
        res.setMsg("保存成功！");
        res.setSuccess(true);
        res.setData(car);
        return res;
    }

    /**
     * 新增档案
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/save/add")
    @ResponseBody
    public ResultEntity saveAdd(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TInsuranceCar data) throws Exception {
        EntityUtils.valid(data);
        this.itInsuranceCarService.saveAdd(data, uid);
        ResultEntity res = new ResultEntity();
        res.setMsg("保存成功！");
        res.setSuccess(true);
        return res;
    }

    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param key
     * @return ResultEntity
     * @throws Exception
     * @desc 查询详情
     * @author 张国凯
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "key", defaultValue = "") String key,
                             @RequestParam(name = "value", defaultValue = "") String value,
                             @RequestParam(name = "companyId", defaultValue = "") String companyId) throws Exception {
        ResultEntity res = new ResultEntity();
        Map map = itInsuranceCarService.info(key, value, uid,companyId);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }




    @RequestMapping("/carInfo")
    @ResponseBody
    public ResultEntity carInfo(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id,
                                @RequestParam(name = "vin", defaultValue = "") String vin) throws Exception {
        ResultEntity res = new ResultEntity();
//        TInsuranceCar insuranceCar= itInsuranceCarService.getCarInfo(id);
        Map data=itInsuranceCarService.getCarInfo(id,vin,uid);
        res.setData(data);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }

    @RequestMapping("/carInfo/vin")
    @ResponseBody
    public ResultEntity carInfoVin(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token,
                                @RequestParam(name = "vin", defaultValue = "") String vin) throws Exception {
        ResultEntity res = new ResultEntity();
        Map data=itInsuranceCarService.getCarInfoByVin(vin,uid);
        res.setData(data);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }

}
